<template>
	<view class="BindPhone">
		<!-- <u-navbar title="" :autoBack="true"></u-navbar> -->
		<view class="main">
			<view class="title">
				绑定手机号
			</view>
			<view class="u-demo-block__content">
				<u-input placeholder="请输入手机号" type="number" v-model="phone" clearable>
					<u--text text="+86" slot="prefix" margin="0 3px 0 0" type="tips"></u--text>
				</u-input>
			</view>
			<view class="u-demo-block__content">
				<u-input placeholder="请输入验证码" type="number">
					<template slot="suffix">
						<u-code ref="uCode" @change="codeChange" seconds="60" changeText="X秒重新获取"></u-code>
						<u-button @tap="getCode" :text="tips" type="success" size="mini"></u-button>
					</template>
				</u-input>
			</view>
			<u-notify ref="uNotify"></u-notify>
			<u-button class="btn" type="primary" text="绑定" @click="submit"></u-button>
		</view>
	</view>
</template>

<script>
	import {
		login,
		addUser
	} from "@/api/api.js"
	export default {
		data() {
			return {
				phone: '',
				tips: '',
			}
		},
		methods: {
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						uni.$u.toast('验证码已发送');
						this.$refs.uNotify.success('验证码为5248')
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 1000);
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			async submit() {
				// #ifdef H5
				let res = await login({
					phone: this.phone
				})
				if (res.code === 300) {
					res = await addUser({
						phone: this.phone
					})
				}
				if (!res.flag) {
					uni.showToast({
						title: '服务异常',
						icon: 'none'
					})
					return
				}
				console.log(res)
				uni.setStorageSync('userInfo', res.data)
				// #endif
				uni.switchTab({
					url: '/pages/my'
				})
			}
		}
	}
</script>

<style lang="scss">
	.BindPhone {
		display: flex;
		height: 100vh;
		background: url('/static/bj.jpg') 50% 0/cover no-repeat;
	}

	.main {
		margin: auto;
		background-color: #fff;
		padding: 100rpx 40rpx;
		border-radius: 20rpx;
		box-shadow: #ccc 2rpx 2rpx 10rpx;

		.title {
			font-size: 40rpx;
			text-align: center;
			margin-bottom: 100rpx;
		}

		.u-demo-block__content {
			border: 2px solid #dadbde;
			margin-bottom: 40rpx;
			border-radius: 10rpx;
		}

		.btn {
			margin-top: 40rpx;
		}
	}
</style>